// Pipit C标准库头文件
#ifndef PIPIT_STDLIB_H
#define PIPIT_STDLIB_H

#ifdef _WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/stat.h>

#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#include <sys/stat.h>
#endif

// 前置声明和类型定义
// 文件信息结构
typedef struct {
    long size;
    int is_directory;
    int is_regular_file;
    long last_modified;
} pipit_file_info_t;

// 字符串分割结构
typedef struct {
    char** tokens;
    int count;
} pipit_string_split_t;

// 行读取器结构
typedef struct {
    FILE* file;
    char buffer[1024];
} pipit_line_reader_t;

// 线程句柄结构
typedef struct pipit_thread {
    void* thread_handle;  // 实际的线程句柄（平台相关）
    int active;
} pipit_thread_t;

// 互斥锁结构
typedef struct pipit_mutex {
    void* mutex_handle;   // 实际的互斥锁句柄（平台相关）
} pipit_mutex_t;

// JSON模块前置声明
typedef struct json_value json_value_t;
typedef struct json_array json_array_t;
typedef struct json_object json_object_t;

// Collections模块前置声明
typedef struct pipit_vector pipit_vector_t;
typedef struct pipit_hashmap pipit_hashmap_t;
typedef struct pipit_list pipit_list_t;
typedef struct pipit_list_node pipit_list_node_t;
typedef struct pipit_stack pipit_stack_t;
typedef struct pipit_queue pipit_queue_t;

// DateTime模块结构
typedef struct {
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;
    int millisecond;
    int timezone_offset;
} pipit_datetime_t;

typedef struct {
    long long days;
    int hours;
    int minutes;
    int seconds;
    int milliseconds;
} pipit_timedelta_t;

typedef struct pipit_timer_t pipit_timer_t;

// IO模块
FILE* pipit_fopen(const char* filename, const char* mode);
int pipit_fclose(FILE* stream);
size_t pipit_fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
size_t pipit_fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream);
int pipit_feof(FILE* stream);
long pipit_file_size(FILE* stream);
char* pipit_read_file(const char* filename);
int pipit_write_file(const char* filename, const char* content);
int pipit_file_exists(const char* filename);
int pipit_remove_file(const char* filename);
int pipit_rename_file(const char* oldname, const char* newname);
char* pipit_getcwd();
int pipit_mkdir(const char* path);
int pipit_rmdir(const char* path);
int pipit_get_file_info(const char* filename, pipit_file_info_t* info);
int pipit_read_buffer(const char* filename, void* buffer, size_t buffer_size);
int pipit_write_buffer(const char* filename, const void* buffer, size_t buffer_size);
pipit_line_reader_t* pipit_line_reader_open(const char* filename);
char* pipit_line_reader_next(pipit_line_reader_t* reader);
void pipit_line_reader_close(pipit_line_reader_t* reader);

// Math模块
int pipit_add(int a, int b);
int pipit_subtract(int a, int b);
int pipit_multiply(int a, int b);
int pipit_divide(int a, int b);
int pipit_modulo(int a, int b);
double pipit_power(double base, double exponent);
double pipit_sqrt(double x);
int pipit_abs(int x);
int pipit_max(int a, int b);
int pipit_min(int a, int b);
int pipit_round(double x);
int pipit_floor(double x);
int pipit_ceil(double x);
int pipit_random();
void pipit_srand(unsigned int seed);
int pipit_random_range(int min, int max);
double pipit_sin(double x);
double pipit_cos(double x);
double pipit_tan(double x);
double pipit_log(double x);
double pipit_log10(double x);
double pipit_exp(double x);
double pipit_random_float();
int pipit_is_prime(int n);
long long pipit_factorial(int n);
int pipit_gcd(int a, int b);
int pipit_lcm(int a, int b);
double pipit_degrees_to_radians(double degrees);
double pipit_radians_to_degrees(double radians);

// String模块
int pipit_string_length(const char* s);
char* pipit_string_concat(const char* s1, const char* s2);
void pipit_string_to_upper(char* s);
void pipit_string_to_lower(char* s);
int pipit_string_compare(const char* s1, const char* s2);
int pipit_string_find(const char* s, const char* substr);
pipit_string_split_t* pipit_string_split(const char* str, const char* delimiter);
char* pipit_string_replace(const char* str, const char* old, const char* new);
char* pipit_string_trim(const char* str);
char* pipit_string_substring(const char* str, int start, int end);
int pipit_string_starts_with(const char* str, const char* prefix);
int pipit_string_ends_with(const char* str, const char* suffix);
int pipit_string_to_int(const char* str);
double pipit_string_to_double(const char* str);
char* pipit_string_format(const char* format, ...);
void pipit_string_split_free(pipit_string_split_t* split);

// OS模块
int pipit_os_system(const char* command);
char* pipit_os_getcwd(char* buffer, size_t size);
int pipit_os_chdir(const char* path);
int pipit_os_mkdir(const char* path);
int pipit_os_remove(const char* path);
int pipit_os_stat(const char* path, struct stat* buf);
long pipit_os_filesize(const char* path);
long pipit_os_timestamp();

// Syscall模块
int pipit_syscall_read_int();
double pipit_syscall_read_float();
char* pipit_syscall_read_string();
char* pipit_syscall_read_line();
void pipit_syscall_print_int(int value);
void pipit_syscall_print_float(double value);
void pipit_syscall_print_string(const char* value);
void pipit_syscall_print_line(const char* value);
char* pipit_syscall_getenv(const char* name);
int pipit_syscall_system(const char* command);

// Concurrent模块
void pipit_sleep(int milliseconds);
pipit_thread_t* pipit_thread_create(void* (*start_routine)(void*), void* arg);
int pipit_thread_join(pipit_thread_t* thread);
long pipit_thread_id();
pipit_mutex_t* pipit_mutex_create();
void pipit_mutex_destroy(pipit_mutex_t* mutex);
int pipit_mutex_lock(pipit_mutex_t* mutex);
int pipit_mutex_unlock(pipit_mutex_t* mutex);

// JSON模块
json_value_t* json_create_null();
json_value_t* json_create_bool(int value);
json_value_t* json_create_number(double value);
json_value_t* json_create_string(const char* value);
json_array_t* json_array_create();
void json_array_append(json_array_t* arr, json_value_t* value);
json_object_t* json_object_create();
void json_object_set(json_object_t* obj, const char* key, json_value_t* value);
char* json_serialize_string(const char* str);

// Collections模块
pipit_vector_t* pipit_vector_create(size_t item_size);
void pipit_vector_push_back(pipit_vector_t* vec, void* item);
void* pipit_vector_get(pipit_vector_t* vec, size_t index);
size_t pipit_vector_size(pipit_vector_t* vec);
void pipit_vector_destroy(pipit_vector_t* vec);

pipit_hashmap_t* pipit_hashmap_create();
void pipit_hashmap_set(pipit_hashmap_t* map, const char* key, void* value);
void* pipit_hashmap_get(pipit_hashmap_t* map, const char* key);

pipit_list_t* pipit_list_create();
void pipit_list_push_back(pipit_list_t* list, void* data);
void* pipit_list_get(pipit_list_t* list, size_t index);
size_t pipit_list_size(pipit_list_t* list);

pipit_stack_t* pipit_stack_create(size_t capacity);
int pipit_stack_push(pipit_stack_t* stack, void* item);
void* pipit_stack_pop(pipit_stack_t* stack);

pipit_queue_t* pipit_queue_create(size_t capacity);
int pipit_queue_enqueue(pipit_queue_t* queue, void* item);
void* pipit_queue_dequeue(pipit_queue_t* queue);

// Crypto模块
char* pipit_crypto_xor_encrypt(const char* text, const char* key);
char* pipit_crypto_caesar_encrypt(const char* text, int shift);
unsigned int pipit_crypto_checksum(const char* data, size_t length);
void pipit_crypto_random_bytes(unsigned char* buffer, size_t length);
char* pipit_crypto_base64_encode(const unsigned char* data, size_t length);
char* pipit_crypto_hmac(const char* message, const char* key);
int pipit_crypto_password_strength(const char* password);

// DateTime模块
long long pipit_datetime_now();
pipit_datetime_t* pipit_datetime_current();
pipit_datetime_t* pipit_datetime_create(int year, int month, int day, int hour, int minute, int second);
char* pipit_datetime_format(pipit_datetime_t* dt, const char* format);
pipit_datetime_t* pipit_datetime_parse(const char* datetime_str, const char* format);
pipit_timedelta_t* pipit_datetime_diff(pipit_datetime_t* dt1, pipit_datetime_t* dt2);
pipit_datetime_t* pipit_datetime_add_delta(pipit_datetime_t* dt, pipit_timedelta_t* delta);
int pipit_datetime_weekday(pipit_datetime_t* dt);
int pipit_datetime_is_leap_year(int year);
int pipit_datetime_days_in_month(int year, int month);
long long pipit_datetime_to_timestamp(pipit_datetime_t* dt);
pipit_datetime_t* pipit_datetime_from_timestamp(long long timestamp);

pipit_timer_t* pipit_timer_create();
void pipit_timer_start(pipit_timer_t* timer);
void pipit_timer_stop(pipit_timer_t* timer);
double pipit_timer_elapsed(pipit_timer_t* timer);

#endif // PIPIT_STDLIB_H